/*
 * PGN_Reader : A simple pgn reader for chess games.
 * Copyright (C) 2011 Laurent BERNABE
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gmail.bernabe.laurent.java_swing.pgn_reader.gui;

import ictk.boardgame.AmbiguousMoveException;
import ictk.boardgame.IllegalMoveException;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class PGNReaderFrame extends JFrame {
	
	public PGNReaderFrame(){
		setTitle("PGN Reader");
		setSize(SIZE);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		boardPanel = new BoardJPanel(this);
		buttonsPanel = new BoardButtonsJPanel(boardPanel);
		setLayout(new BorderLayout());
		add(boardPanel, BorderLayout.CENTER);
		add(movesAreaScrollPane, BorderLayout.EAST);
		add(buttonsPanel, BorderLayout.SOUTH);
		
		setJMenuBar(buildJMenuBar());
	}

	public BoardButtonsJPanel getButtonsPanel(){
		return buttonsPanel;
	}
	
	public MovesJTextArea getTextArea(){
		return movesArea;
	}
	
	public BoardJPanel getGraphicPanel(){
		return boardPanel;
	}
	
	private JMenuBar buildJMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		JMenuItem fileOpen_menuItem = new JMenuItem("Open");
		fileMenu.add(fileOpen_menuItem);
		fileOpen_menuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser(currentFileChosserDirectory);
				fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PGN file (*.pgn)", new String[]{"pgn"}));
				fileChooser.setAcceptAllFileFilterUsed(false);
				int result = fileChooser.showOpenDialog(PGNReaderFrame.this);
				if (result == JFileChooser.APPROVE_OPTION) {
					currentFileChosserDirectory = fileChooser.getCurrentDirectory();
					File fileToOpen = fileChooser.getSelectedFile();
					try {
						boardPanel.loadPGN(fileToOpen);
					} catch (IllegalMoveException e1) {
						giveErrorToUser("One game in the pgn file had a illegal move !!!");
						e1.printStackTrace();
					} catch (AmbiguousMoveException e1) {
						giveErrorToUser("One move in one game of the pgn file was subject to several interpretations !!!");
						e1.printStackTrace();
					} catch (FileNotFoundException e1) {
						giveErrorToUser("File could not be found !!!");
						e1.printStackTrace();
					}
				}
			}
			
		});
		
		return menuBar;
	}
	
	private void giveErrorToUser(String errorMessage) {
		JOptionPane.showMessageDialog(this, errorMessage, "Error !!!", JOptionPane.ERROR_MESSAGE);
	}
	
	private static final long serialVersionUID = 1L;
	private BoardButtonsJPanel buttonsPanel;
	private MovesJTextArea movesArea = new MovesJTextArea();
	private BoardJPanel boardPanel;
	private JScrollPane movesAreaScrollPane = new JScrollPane(movesArea);
	private final static Dimension SIZE = new Dimension(700, 470);
	
	private File currentFileChosserDirectory;
}
